package com.ohos.carbon.slice.demos;

import com.ohos.carbon.ResourceTable;
import com.ohos.carbon.model.ProfileDataBean;
import com.ohos.carbon.provider.ProfileAdapter;
import com.ohos.carbon.slice.MainAbilitySlice;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.Image;
import ohos.agp.components.ListContainer;
import ohos.agp.components.Text;
import ohos.agp.components.element.ElementScatter;
import ohos.agp.utils.Color;

import java.util.ArrayList;
import java.util.Map;

import static com.ohos.carbon.slice.MainAbilitySlice.pf;

public class ProfileSlice extends AbilitySlice {

    private Image back_button, image1, image2, avatar, positioning, emailimg, back;
    private Text text_title;
    private ListContainer img_list, avatar_list;
    private ArrayList<ProfileDataBean> imglist;
    private ArrayList<ProfileDataBean> avatarlist;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_profile);
        initData();
        initView();

        MainAbilitySlice.all = (Map<String, Integer>) pf.getAll();
        if (MainAbilitySlice.all.size() != 0) {
            if (MainAbilitySlice.all.containsKey("Profile")) {
                for (Map.Entry<String, Integer> entry : MainAbilitySlice.all.entrySet()) {
                    if (entry.getKey().equals("Profile")) {
                        int integer = entry.getValue();
                        integer++;
                        pf.putInt("Profile", integer);
                        pf.flushSync();
                    }
                }
            } else {
                pf.putInt("Profile", 1);
                pf.flushSync();
            }
        } else {
            pf.putInt("Profile", 1);
            pf.flushSync();
        }
    }

    private void initData() {
        imglist = new ArrayList<>();
        imglist.add(new ProfileDataBean(ResourceTable.Media_randomdata_background3, null));
        imglist.add(new ProfileDataBean(ResourceTable.Media_randomdata_background2, null));
        imglist.add(new ProfileDataBean(ResourceTable.Media_randomdata_background1, null));
        imglist.add(new ProfileDataBean(ResourceTable.Media_randomdata_background0, null));
        imglist.add(new ProfileDataBean(ResourceTable.Media_randomdata_background4, null));

        avatarlist = new ArrayList<>();
        avatarlist.add(new ProfileDataBean(ResourceTable.Media_randomdata_man8, "Mark"));
        avatarlist.add(new ProfileDataBean(ResourceTable.Media_randomdata_woman9, "Daisy"));
        avatarlist.add(new ProfileDataBean(ResourceTable.Media_randomdata_man3, "Andre"));
        avatarlist.add(new ProfileDataBean(ResourceTable.Media_randomdata_woman3, "Linda"));
    }

    private void initView() {
        back_button = (Image) findComponentById(ResourceTable.Id_back_button);
        back_button.setImageElement(ElementScatter.getInstance(getContext()).parse(ResourceTable.Graphic_white_carbon_uparrow));
        image1 = (Image) findComponentById(ResourceTable.Id_image1);
        image2 = (Image) findComponentById(ResourceTable.Id_image2);
        image1.setVisibility(Component.HIDE);
        image2.setVisibility(Component.HIDE);
        text_title = (Text) findComponentById(ResourceTable.Id_title_text);
        text_title.setText("Profile");
        text_title.setTextColor(Color.WHITE);
        back_button.setClickedListener(component -> onBackPressed());


        avatar = (Image) findComponentById(ResourceTable.Id_avatar);
        avatar.setCornerRadius(1000);

        positioning = (Image) findComponentById(ResourceTable.Id_positioning);
        positioning.setImageElement(ElementScatter.getInstance(getContext()).parse(ResourceTable.Graphic_ic_location_on_black_24dp));

//        back = (Image) findComponentById(ResourceTable.Id_back);
//        back.setCornerRadius(1000);

        emailimg = (Image) findComponentById(ResourceTable.Id_emailimg);
        emailimg.setImageElement(ElementScatter.getInstance(getContext()).parse(ResourceTable.Graphic_ic_email_black_24dp));


        img_list = (ListContainer) findComponentById(ResourceTable.Id_img_list);
        avatar_list = (ListContainer) findComponentById(ResourceTable.Id_avatar_list);
        back_button.setImageElement(ElementScatter.getInstance(getContext()).parse(ResourceTable.Graphic_carbon_uparrow));
        img_list.setItemProvider(new ProfileAdapter(imglist, getContext()));
        avatar_list.setItemProvider(new ProfileAdapter(avatarlist, getContext()));
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
